'use strict';
const path = require('path');

function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  pages: {
    index: {
      // page 的入口
      entry: 'src/main.js',
      // 模板来源
      template: 'public/index.html',
      // 在 dist/index.html 的输出
      filename: 'index.html',
      // 当使用 title 选项时，
      // template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
      title: 'kaitian',
      // 在这个页面中包含的块，默认情况下会包含
      // 提取出来的通用 chunk 和 vendor chunk。
      chunks: ['chunk-vendors', 'chunk-common', 'index', 'chunk-brace', 'chunk-element', 'chunk-oss', 'chunk-plotly']
    }
  },
  publicPath: './',
  outputDir: 'kaitian',
  assetsDir: 'assets',
  lintOnSave: true,
  runtimeCompiler: true,
  productionSourceMap: false,
  configureWebpack: {
    optimization: {
      splitChunks: {
        chunks: 'all',
        maxInitialRequests: Infinity,
        minSize: 30000,
        cacheGroups: {
          brace: {
            name: 'chunk-brace',
            test: /[\\/]node_modules[\\/]brace[\\/]/,
            reuseExistingChunk: true
          },
          element: {
            name: 'chunk-element',
            test: /[\\/]node_modules[\\/]element-ui[\\/]/,
            reuseExistingChunk: true
          },
          aliOss: {
            name: 'chunk-oss',
            test: /[\\/]node_modules[\\/]ali-oss[\\/]/,
            reuseExistingChunk: true
          },
          plotly: {
            name: 'chunk-plotly',
            test: /[\\/]node_modules[\\/]plotly\.js-dist-min[\\/]/,
            reuseExistingChunk: true
          }
        }
      }
    }
  },
  css: {
    extract: true,
    sourceMap: false,
    loaderOptions: {},
    requireModuleExtension: true
  },
  devServer: {
    hot: true,
    port: 80,
    https: false,
    open: true,
    proxy: {
      '/api': { // 本地
        target: 'http://localhost:13232/',
        ws: true,
        changeOrigin: true,
        pathRewrite: {
          '^/api': '/'
        }
      }
    },
    headers: {
      'Access-Control-Allow-Origin': '*'
    }
  },
  chainWebpack: (config) => {
    config.plugins.delete('prefetch-index');
    config.plugins.delete('preload-index');
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/assets/icons'))
      .end();
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/assets/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end();
  }
};
